import { queryPage, queryInfoById, create, lock, update, queryRoleList, queryGroupList, allotRoles, allotGroups, resetOtherPwd, queryFireRoomList, deletePerson } from './service';
import { routerRedux } from 'dva/router';
import { queryDictList } from '@/services/dict';
import { notification } from 'antd';
import assgin from 'lodash/assign';

function setList(list, index) {
  list[index].status = list[index].status === 'lock' ? 'normal' : 'lock';
  return list;
}

export default {
  namespace: 'person',

  state: {
    list: { data: [] },
    details: {},
    cond: {},
    newPwd: '',
  },

  effects: {
    *initCond(_, { put }) {
      yield put({ type: 'setCond', payload: {} });
      yield put({ type: 'queryPage' })
    },
    *deletePerson({ payload }, { call, put }) {
      yield call(deletePerson, payload)
      notification.success({
        message: '注销成功',
        description: '人员注销成功'
      })
     // yield put(routerRedux.push('/data/person'));

    },
    *updateCond({ payload }, { put }) {
      yield put({ type: 'setCond', payload });
      yield put({ type: 'queryPage' })
    },
    *queryPage({ payload = { pageNum: 1, pageSize: 10 }  }, { call, put, select }) {
      const cond = yield select(state => state.person.cond);
      const params = { ...payload, ...cond };
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'setPage', payload: { list, total: +total } })
    },
    *queryInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload);
      yield put({ type: 'setInfo', payload: response })
    },
    *queryEditInfoById({ payload }, { call, put }) {
      const response = yield call(queryInfoById, payload.id);
      // const items = {};

      // payload.keys.forEach(item => {
      //   items[item] = response[item]
      // })
      yield put({ type: 'setEditInfo', payload: response })
    },
    *create({ payload }, { call, put }) {
      yield call(create, payload);
      notification.success({
        message: '新增成功',
        description: '人员信息新增成功'
      })
      yield put(routerRedux.push('/data/person'));
    },
    *update({ payload }, { call, put }) {
      yield call(update, payload);
      notification.success({
        message: '修改成功',
        description: '人员信息修改成功'
      })
      yield put(routerRedux.push('/data/person'));
     
    },
    *lock({ payload }, { all, call, put }) {
      yield all([
        call(lock, payload.id),
        put({ type: 'setOneLock', payload: payload.ind })
      ]);
    },
    *queryRoleList(_, { call, put }) {
      const response = yield call(queryRoleList);
      yield put({
        type: 'setRoleList',
        payload: response
      })
    },   
     *queryFireRoomList(_, { call, put }) {
      const response = yield call(queryFireRoomList);
      yield put({
        type: 'setFireRoomList',
        payload: response
      })
    },
    *queryGroupList(_, { call, put }) {
      const response = yield call(queryGroupList);
      yield put({
        type: 'setGroupList',
        payload: response
      })
    },
    *queryAuthTree({ payload }, { take, put, select }) {
      yield put({ type: 'queryInfoById', payload: payload.id });
      yield put({ type: 'queryRoleList' });
      yield take('queryInfoById/@@end')
      const details = yield select(state => state.person.details)
      payload.callback(details)
    },
    *allotRoles({ payload }, { call, put }) {
      yield call(allotRoles, payload);
      yield put(routerRedux.push('/data/person'));
    },
    *queryGroupTree({ payload }, { take, put, select }) {
      yield put({ type: 'queryInfoById', payload: payload.id })
      yield put({ type: 'queryGroupList' })
      yield take('queryInfoById/@@end')
      const details = yield select(state => state.person.details)
      payload.callback(details)
    },
    *allotGroups({ payload }, { call, put }) {
      yield call(allotGroups, payload);
      yield put(routerRedux.push('/data/person'));
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *init({ payload }, { put }) {
      yield put({ type: 'upload/setImgValue', payload: { url: payload.url } });
      yield put({ type: 'queryDict', payload: payload.dict });
    },
    *resetOtherPwd({ payload }, { call, put }) {
      const response = yield call(resetOtherPwd , payload);
      yield put({ type: 'setNewPwd', payload: response });
    }
  },

  reducers: {
    setCond(state, action) {
      return {
        ...state,
        cond: action.payload
      }
    },
    setPage(state, action) {
      return {
        ...state,
        list: {
          data: action.payload.list,
          dataCnt: action.payload.total
        }
      }
    },
    setInfo(state, action) {
      return {
        ...state,
        details: action.payload
      }
    },
    setEditInfo(state, action) {
      return {
        ...state,
        editDetails: action.payload
      }
    },
    setOneLock(state, action) {
      return {
        ...state,
        list: {
          ...state.list,
          data: setList(state.list.data, action.payload)
        }
      }
    },
    setRoleList(state, action) {
      return {
        ...state,
        roles: action.payload
      }
    },  
    setFireRoomList(state, action) {
      return {
        ...state,
        roomList: action.payload
      }
    },
    setGroupList(state, action) {
      return {
        ...state,
        groups: action.payload
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: assgin(state.dict, action.payload)
      }
    },
    setNewPwd(state, action) {
      return {
        ...state,
        newPwd: action.payload
      }
    }
  }
}
